package ckw.gui;

import java.util.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import ckw.database.model.Election;
import core.util.Converter;
import core.util.DialogPositioner;

public class WzdPgAddEditElectionBasicData extends ValidationWizardPage {

	private boolean addElection;
	private Election election;

	private Text txtName;
	private Text txtDescription;
	private Text txtStartDate;
	private Text txtEndDate;

	/**
	 * Create the wizard.
	 */
	public WzdPgAddEditElectionBasicData(boolean addElection, Election election) {
		super("WzdPgAddEditElectionBasicData");

		this.addElection = addElection;
		this.election = election;

		if (addElection) {
			setTitle("Add Elections basic data");
		} else {
			setTitle("Edit Elections basic data");
		}

		setDescription("Set Elections basic data");
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(final Composite parent) {
		Composite cmpMain = new Composite(parent, SWT.NULL);

		setControl(cmpMain);
		cmpMain.setLayout(new GridLayout(3, false));

		Label lblName = new Label(cmpMain, SWT.NONE);
		lblName.setText("Name:");

		txtName = new Text(cmpMain, SWT.BORDER);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

		Label lblDescription = new Label(cmpMain, SWT.NONE);
		lblDescription.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		lblDescription.setText("Description:");

		txtDescription = new Text(cmpMain, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_txtDescription = new GridData(GridData.FILL_BOTH);
		gd_txtDescription.horizontalSpan = 2;
		gd_txtDescription.heightHint = 100;
		gd_txtDescription.grabExcessVerticalSpace = false;
		txtDescription.setLayoutData(gd_txtDescription);

		Label lblStartDate = new Label(cmpMain, SWT.NONE);
		lblStartDate.setText("Start date:");

		txtStartDate = new Text(cmpMain, SWT.BORDER);
		txtStartDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnSelectStartDate = new Button(cmpMain, SWT.NONE);
		btnSelectStartDate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Date startDate = Converter.stringToDate(txtStartDate.getText());
				
				DateTimeSelectorWindow dateTimeSelectorWindow = new DateTimeSelectorWindow(parent.getShell(), startDate);
				DialogPositioner.centerShell(dateTimeSelectorWindow.shell);
				Date resultDate = dateTimeSelectorWindow.open();
				
				txtStartDate.setText(Converter.dateToString(resultDate));
			}
		});
		btnSelectStartDate.setText("...");

		Label lblEndDate = new Label(cmpMain, SWT.NONE);
		lblEndDate.setText("End date:");

		txtEndDate = new Text(cmpMain, SWT.BORDER);
		txtEndDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Button btnSelectEndDate = new Button(cmpMain, SWT.NONE);
		btnSelectEndDate.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Date startDate = Converter.stringToDate(txtEndDate.getText());
				
				DateTimeSelectorWindow dateTimeSelectorWindow = new DateTimeSelectorWindow(parent.getShell(), startDate);
				DialogPositioner.centerShell(dateTimeSelectorWindow.shell);
				Date resultDate = dateTimeSelectorWindow.open();
				
				txtEndDate.setText(Converter.dateToString(resultDate));
			}
		});
		btnSelectEndDate.setText("...");

		initValues();
		setValidationListeners();
	}

	private void initValues() {
		if (addElection) {
			setPageComplete(false);
		} else {
			txtName.setText(election.getName());
			txtDescription.setText(election.getDescription());
			txtStartDate.setText(Converter.dateToString(election.getStartDate()));
			txtEndDate.setText(Converter.dateToString(election.getEndDate()));
		}
	}
	
	private void setValidationListeners() {
		txtName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtDescription.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtStartDate.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtEndDate.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
	}

	@Override
	protected void validate() {
		String name = txtName.getText().trim();
//		String description = txtDescription.getText().trim();
		String startDateStr = txtStartDate.getText().trim();
		String endDateStr = txtEndDate.getText().trim();

		if (MainWindow.isInDebugMode()) {
			clearErrorState();
			return;
		}
		// error
		if (name.length() == 0) {
			setErrorState("Name cannot be empty.");
			return;
		}
		// if (description.length() == 0) {
		// setPageErrorMessage("Description cannot be empty.");
		// return;
		// }
		if (startDateStr.length() == 0) {
			setErrorState("Start date cannot be empty.");
			return;
		}
		if (Converter.stringToDate(startDateStr) == null) {
			setErrorState("Wrong start date format. Accepted format is: " + Converter.getDateFormatPattern());
			return;
		}
		if (endDateStr.length() == 0) {
			setErrorState("End date cannot be empty.");
			return;
		}
		if (Converter.stringToDate(endDateStr) == null) {
			setErrorState("Wrong end date format. Accepted format is: " + Converter.getDateFormatPattern());
			return;
		}
		Date startDate = Converter.stringToDate(startDateStr);
		Date endDate = Converter.stringToDate(endDateStr);
		
		if (endDate.after(startDate) == false) {
			setErrorState("Start date must be before End date.");
			return;
		}

		// ok
		clearErrorState();
	}

	@Override
	protected void update() {
		election.setName(txtName.getText().trim());
		election.setDescription(txtDescription.getText().trim());
		election.setStartDate(Converter.stringToDate(txtStartDate.getText().trim()));
		election.setEndDate(Converter.stringToDate(txtEndDate.getText().trim()));
	}
}
